package org.app.Service.Impl;

import jakarta.annotation.Resource;
import org.app.Entity.ExercisePlan;
import org.app.Entity.Like;
import org.app.Entity.comm.Result;
import org.app.Mapper.ILikeMapper;
import org.app.Service.ILikeService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LikeService implements ILikeService {
    @Resource
    private ILikeMapper likeMapper;

    @Override
    public Result<Integer> createCollection(Like like){
        Result result = new Result();
        Like like1 = likeMapper.getByCidAndPid(like.getC_id(), like.getP_id());
        if(like1 == null){
            result.setCode(200);
            result.setData(likeMapper.createCollection(like));
            result.setMessage("收藏成功");
        } else if (like1 != null) {
            result.setCode(501);
            result.setData(null);
            result.setMessage("已经收藏过了");
        } else {
            result.setCode(500);
            result.setData(null);
            result.setMessage("收藏失败");
        }

        return result;
    }
    @Override
    public Result<List<ExercisePlan>> getAllByCID(String c_id){
        Result result = new Result();
        if(c_id != null){
            result.setCode(200);
            result.setData(likeMapper.getAllByCID(c_id));
            result.setMessage("查询成功");
        } else {
            result.setCode(500);
            result.setData(null);
            result.setMessage("查询失败");
        }

        return result;
    }
    @Override
    public Result<Integer> deleteByPidAndCid(String c_id,String p_id){
        Result result = new Result();
        Like like = likeMapper.getByCidAndPid(c_id,p_id);
        if(like != null){
            result.setCode(200);
            result.setData(likeMapper.deleteByPidAndCid(like));
            result.setMessage("删除成功");
        } else {
            result.setCode(500);
            result.setData(null);
            result.setMessage("删除失败");
        }

        return result;
    }
}
